Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-mocks

Package Overview
Dependencies
Maintainers
1
Versions
105
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-mocks

AngularJS mocks for testing

  • 1.8.3
  • next
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
136K
decreased by-17.01%
Maintainers
1
Weekly downloads
 
Created

What is angular-mocks?

The angular-mocks package provides mocking and testing utilities for AngularJS applications. It includes modules, services, and methods to facilitate unit testing and end-to-end testing of AngularJS code.

What are angular-mocks's main functionalities?

Mocking HTTP Requests

This feature allows you to mock HTTP requests in your AngularJS application. The code sample demonstrates how to use $httpBackend to intercept a GET request to '/api/data' and respond with a status code of 200 and a JSON object.

angular.module('myApp', ['ngMockE2E'])
  .run(function($httpBackend) {
    $httpBackend.whenGET('/api/data').respond(200, { key: 'value' });
  });

Injecting and Mocking Services

This feature allows you to inject and mock services in your unit tests. The code sample shows how to inject MyService and $httpBackend, set up an expectation for an HTTP GET request, and verify the response.

describe('MyService', function() {
  var MyService, $httpBackend;

  beforeEach(module('myApp'));
  beforeEach(inject(function(_MyService_, _$httpBackend_) {
    MyService = _MyService_;
    $httpBackend = _$httpBackend_;
  }));

  it('should fetch data', function() {
    $httpBackend.expectGET('/api/data').respond(200, { key: 'value' });
    MyService.getData().then(function(response) {
      expect(response.data.key).toBe('value');
    });
    $httpBackend.flush();
  });
});

Spying on Methods

This feature allows you to spy on methods to check if they have been called and with what arguments. The code sample demonstrates how to spy on the getData method of MyService and verify that it has been called when MyController is instantiated.

describe('MyController', function() {
  var $controller, $rootScope, MyService;

  beforeEach(module('myApp'));
  beforeEach(inject(function(_$controller_, _$rootScope_, _MyService_) {
    $controller = _$controller_;
    $rootScope = _$rootScope_;
    MyService = _MyService_;
  }));

  it('should call MyService.getData', function() {
    spyOn(MyService, 'getData').and.callThrough();
    var $scope = $rootScope.$new();
    $controller('MyController', { $scope: $scope });
    expect(MyService.getData).toHaveBeenCalled();
  });
});

Other packages similar to angular-mocks

Keywords

FAQs

Package last updated on 07 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc